<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserModel
 *
 * @author Rung-PC
 */
namespace Home\Model;
use Think\Model;
class AdminUserModel extends Model{
         public function find($table,$where,$field=''){
           $admin_user=M($table);
           return $admin_user->where($where)->field($field)->find();
        }
   
        public function select($table,$where='',$field='',$order='',$firstRow='',$listRows=''){
           $admin_user=M($table);
           return $admin_user->where($where)->field($field)->order($order)->limit($firstRow.','.$listRows)->select();
        }
    
        public function lastId(){
            $admin_user = M('admin_user');
            return $admin_user->order('user_id desc')->limit('1')->find();
        }
        
        public function countAll($table,$data=''){
            $admin_user = M($table);
            return $admin_user->where($data)->count();
        }
        
        public function insertAll($table,$dataList){
            $model = M($table);
            $model->addAll($dataList);
        }
        
        public function insertOne($table,$dataList){
            $model = M($table);
            return $model->add($dataList);
        }

        public function update($table,$data,$key,$value){
            $admin_user = M($table);
            return $admin_user-> where($data)->setField($key,$value);
        }
        
        public function plus($table,$data,$key,$val){
            $model = M($table);
            $model->where($data)->setInc($key,$val);
        }
        
        public function del($table,$data){
            $model = M($table);
            return $model->where($data)->delete();
        }
        
        public function change($table,$where,$data){
            $model = M($table);
            return $model->where($where)->setField($data);
        }
        
        public function showOrder($where = 1,$firstRow,$listRows){  //展示可登录用户订单
            $model = M(); 
            $sql = "SELECT * FROM `ecs_order_info` WHERE `user_id` IN (SELECT user_id FROM `ecs_admin_user` WHERE STATUS = 1) ";
            if($where == ''){
                $sql .= " ORDER BY order_status asc LIMIT ".$firstRow.",".$listRows;
            }else{
                $sql .= "AND ".$where." ORDER BY order_status asc LIMIT ".$firstRow.",".$listRows;
            }
            return $model->query($sql);
        }
        
        public function totals(){
            $model = M();
            $sql = "SELECT COUNT(*) FROM `ecs_order_info` WHERE user_id IN (SELECT user_id from `ecs_admin_user` WHERE status = 1)";
            return $model->query($sql);
        }
}
